package com.senven.books.modules.booksManager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.senven.books.framework.web.model.LoginBody;
import com.senven.books.modules.booksManager.entity.Book;
import com.senven.books.modules.booksManager.entity.form.BookForm;
import com.senven.books.modules.booksManager.entity.param.BookParam;
import com.senven.books.modules.booksManager.entity.vo.BookVo;
import com.senven.books.modules.booksManager.service.BookManagerService;
import com.senven.books.utils.ResultEnum;
import com.senven.books.utils.ResultVO;
import com.senven.books.utils.ResultVOUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author : longshengyi
 * @description :
 * @createDate : 2024/7/23 0:33
 */
@Api(tags = {"图书管理"})
@RestController
@RequestMapping("/api/books")
public class BookMangerController {

    @Autowired
    private BookManagerService bookManagerService;

    @ApiOperation(value = "图书分页查询")
    @GetMapping
    public ResultVO getBooksByPage(
            @ApiParam(value = "当前页", name = "current", required = true, defaultValue = "1") @RequestParam(required = true) Integer current,
            @ApiParam(value = "每页条数", name = "pageSize", required = true, defaultValue = "10") @RequestParam(required = true) Integer pageSize,
            @ApiParam(value = "标题", name = "title", required = false) @RequestParam(required = false) String title) {
        Page<BookVo> page = new Page<>(current, pageSize);
        BookParam bookParam = new BookParam();
        bookParam.setTitle(title);
        return ResultVOUtil.success(ResultEnum.SUCCESS.getCode(), "获取成功", bookManagerService.getBooksByPage(page,bookParam));
    }

    @ApiOperation(value = "根据id获取图书信息")
    @GetMapping("/{id}")
    public ResultVO getBookById(@ApiParam(value = "图书id", name = "id", required = true) @RequestParam(required = true) Long id) {
        return ResultVOUtil.success(ResultEnum.SUCCESS.getCode(), "获取成功", bookManagerService.getBookVoById(id));
    }

    @ApiOperation(value = "根据id删除图书信息")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('ROLE_SUPER_ADMIN')")
    public ResultVO deleteBookById(@ApiParam(value = "图书id", name = "id", required = true) @RequestParam(required = true) Long id) {
        return ResultVOUtil.success(ResultEnum.SUCCESS.getCode(), "删除成功", bookManagerService.deleteById(id));
    }

    @ApiOperation(value = "修改图书")
    @PutMapping
    @ApiImplicitParam(name = "bookForm", value = "图书form表单", required = true, dataType = "BookForm")
    @PreAuthorize("hasAuthority('ROLE_SUPER_ADMIN')")
    public ResultVO updateBook(@RequestBody BookForm bookForm) {
        Book book = bookForm.toPo(Book.class);
        if (book.getId() == null){
            return ResultVOUtil.fail("id不能为空");
        }
        if (StringUtils.isEmpty(book.getTitle())){
            return ResultVOUtil.fail("书名不能为空");
        }
        else if (StringUtils.isEmpty(book.getAuthor())){
            return ResultVOUtil.fail("作者不能为空");
        }
        else if (StringUtils.isEmpty(book.getCategory())){
            return ResultVOUtil.fail("类别不能为空");
        }
        else if (StringUtils.isEmpty(book.getPrice())){
            return ResultVOUtil.fail("价格不能为空");
        }
        else if (book.getPublishedDate() == null){
            return ResultVOUtil.fail("出版时间不能为空");
        }
        return ResultVOUtil.success(ResultEnum.SUCCESS.getCode(), "修改成功", bookManagerService.updateBookBook(book));
    }

    @ApiOperation(value = "新增图书")
    @PostMapping
    @ApiImplicitParam(name = "bookForm", value = "图书form表单", required = true, dataType = "BookForm")
    @PreAuthorize("hasAuthority('ROLE_SUPER_ADMIN')")
    public ResultVO createBook(@RequestBody BookForm bookForm) {
        Book book = bookForm.toPo(Book.class);
        if (StringUtils.isEmpty(book.getTitle())){
            return ResultVOUtil.fail("书名不能为空");
        }
        else if (StringUtils.isEmpty(book.getAuthor())){
            return ResultVOUtil.fail("作者不能为空");
        }
        else if (StringUtils.isEmpty(book.getCategory())){
            return ResultVOUtil.fail("类别不能为空");
        }
        else if (StringUtils.isEmpty(book.getPrice())){
            return ResultVOUtil.fail("价格不能为空");
        }
        else if (book.getPublishedDate() == null){
            return ResultVOUtil.fail("出版时间不能为空");
        }
        return ResultVOUtil.success(ResultEnum.SUCCESS.getCode(), "新增成功", bookManagerService.createBook(book));
    }

}
